
const _interopRequireDefault = require('@babel/runtime/helpers/interopRequireDefault');

exports.__esModule = true;
exports.default = void 0;

const _babelHelperVueJsxMergeProps = _interopRequireDefault(require('@vue/babel-helper-vue-jsx-merge-props'));

const _extends2 = _interopRequireDefault(require('@babel/runtime/helpers/extends'));

const _utils = require('../utils');

const _functional = require('../utils/functional');

const _event = require('../utils/dom/event');

const _field = _interopRequireDefault(require('../field'));

// Utils
// Components
const _createNamespace = (0, _utils.createNamespace)('search');
const createComponent = _createNamespace[0];
const bem = _createNamespace[1];
const t = _createNamespace[2];

function Search(h, props, slots, ctx) {
  function Label() {
    if (slots.label || props.label) {
      return h('div', {
        class: bem('label'),
      }, [slots.label ? slots.label() : props.label]);
    }
  }

  function Action() {
    if (!props.showAction) {
      return;
    }

    function onCancel() {
      if (slots.action) {
        return;
      }

      (0, _functional.emit)(ctx, 'input', '');
      (0, _functional.emit)(ctx, 'cancel');
    }

    return h('div', {
      class: bem('action'),
      attrs: {
        role: 'button',
        tabindex: '0',
      },
      on: {
        click: onCancel,
      },
    }, [slots.action ? slots.action() : props.actionText || t('cancel')]);
  }

  const fieldData = {
    attrs: ctx.data.attrs,
    on: (0, _extends2.default)({}, ctx.listeners, {
      keypress: function keypress(event) {
        // press enter
        if (event.keyCode === 13) {
          (0, _event.preventDefault)(event);
          (0, _functional.emit)(ctx, 'search', props.value);
        }

        (0, _functional.emit)(ctx, 'keypress', event);
      },
    }),
  };
  const inheritData = (0, _functional.inherit)(ctx);
  inheritData.attrs = undefined;
  return h('div', (0, _babelHelperVueJsxMergeProps.default)([{
    class: bem({
      'show-action': props.showAction,
    }),
    style: {
      background: props.background,
    },
  }, inheritData]), [slots.left == null ? void 0 : slots.left(), h('div', {
    class: bem('content', props.shape),
  }, [Label(), h(_field.default, (0, _babelHelperVueJsxMergeProps.default)([{
    attrs: {
      type: 'search',
      border: false,
      value: props.value,
      leftIcon: props.leftIcon,
      rightIcon: props.rightIcon,
      clearable: props.clearable,
    },
    scopedSlots: {
      'left-icon': slots['left-icon'],
      'right-icon': slots['right-icon'],
    },
  }, fieldData]))]), Action()]);
}

Search.props = {
  value: String,
  label: String,
  rightIcon: String,
  actionText: String,
  showAction: Boolean,
  background: String,
  shape: {
    type: String,
    default: 'square',
  },
  clearable: {
    type: Boolean,
    default: true,
  },
  leftIcon: {
    type: String,
    default: 'search',
  },
};

const _default = createComponent(Search);

exports.default = _default;
